package com.ymatou.iapi.fastturnover.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.FastTurnoverService;

import com.ymttest.database.model.fastturnover.TurnoverApply;
import com.ymttest.database.model.fastturnover.TurnoverApplyDetail;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyDetailWrapper;
import com.ymttest.database.sqlwapper.fastturnover.TurnoverApplyWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 取消订单 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_autoRepayJob {
	
	private static TurnoverApplyWrapper turnoverApplyWrapper = new TurnoverApplyWrapper();
	private static TurnoverApplyDetailWrapper turnoverApplyDetailWrapper = new TurnoverApplyDetailWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("自动还款job");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("autoRepayJob");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_autoRepayJob_001() {
		Logger.start(true, "自动还款失败，余额不足");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			FastTurnoverService.addTurnoverApply();
			
			String applyNo = turnoverApplyWrapper.selectBySellerIdAndPayStatus(20227567L, 0).getApply_no();
			String applyDetailNo = turnoverApplyDetailWrapper
					.selectBySellerIdAndApplyNo(20227567L, applyNo)
					.getApply_detail_no();
			
			String payBizNo = TestDataManager.randomStr(10);
			
			FastTurnoverService.verifyTurnoverApply(Arrays.asList(applyNo));
			
			FastTurnoverService.onApplyDetailExportPayEvent(applyDetailNo);
			
			FastTurnoverService.onApplyDetailImportPayEvent(applyDetailNo, payBizNo, 1);
			
			String accountid1 = AccountCallServiceV2.getAccountId(20227567, 1);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					"10000"), BigDecimal.ZERO);
			
			FastTurnoverService.autoRepayJob();
			
			Thread.sleep(3000);

			// 验证数据库结果
			TurnoverApply actualResult = turnoverApplyWrapper.selectByPrimaryKey(applyNo);
			
			// 会记录类似日志“买手20227567人民币账户金额1.00小于待还款金额10001.00，不能自动还款”
			Logger.verifyEquals(1, actualResult.getPay_status(),
					"验证Pay_status");
//			Logger.verifyEquals(2, actualResult.getRepay_status(),
//					"验证Repay_status");

			TurnoverApplyDetail actualTurnoverApplyDetail = turnoverApplyDetailWrapper.selectBySellerIdAndApplyNo(20227567L,
					applyNo);
			
			Logger.verifyEquals(6, actualTurnoverApplyDetail.getStatus(),
					"验证status");
			
			turnoverApplyWrapper.updateToFinishByApplyNo(applyNo);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_autoRepayJob_002() {
		Logger.start(true, "自动还款成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			FastTurnoverService.addTurnoverApply();
			
			String applyNo = turnoverApplyWrapper.selectBySellerIdAndPayStatus(20227567L, 0).getApply_no();
			String applyDetailNo = turnoverApplyDetailWrapper
					.selectBySellerIdAndApplyNo(20227567L, applyNo)
					.getApply_detail_no();
			
			String payBizNo = TestDataManager.randomStr(10);
			
			FastTurnoverService.verifyTurnoverApply(Arrays.asList(applyNo));
			
			FastTurnoverService.onApplyDetailExportPayEvent(applyDetailNo);
			
			FastTurnoverService.onApplyDetailImportPayEvent(applyDetailNo, payBizNo, 1);
			
			FastTurnoverService.autoRepayJob();
			
			Thread.sleep(3000);

			// 验证数据库结果
			TurnoverApply actualResult = turnoverApplyWrapper.selectByPrimaryKey(applyNo);
			
			Logger.verifyEquals(1, actualResult.getPay_status(),
					"验证Pay_status");
			Logger.verifyEquals(1, actualResult.getRepay_status(),
					"验证Repay_status");
			DataUtil.verifyDecimalEquals(new BigDecimal("10001.00"),
					actualResult.getRepay_amount(), "验证返款金额");

			TurnoverApplyDetail actualTurnoverApplyDetail = turnoverApplyDetailWrapper.selectBySellerIdAndApplyNo(20227567L,
					applyNo);
			
			Logger.verifyEquals(7, actualTurnoverApplyDetail.getStatus(),
					"验证status");
			
			turnoverApplyWrapper.updateToFinishByApplyNo(applyNo);
			
			AccountWapper accountWapper = new AccountWapper();
			
			DataUtil.verifyDecimalEquals(actualResult.getApply_total_amount().negate(),
					accountWapper.selectAccountEntry(20227567, applyNo, 1).get(0)
							.getAmount(), "验证账户流水");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_autoRepayJob_003() {
		Logger.start(true, "自动还款失败，人民币账户-冻结账户导致余额不足");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			FastTurnoverService.addTurnoverApply();
			
			String applyNo = turnoverApplyWrapper.selectBySellerIdAndPayStatus(3383L, 0).getApply_no();
			String applyDetailNo = turnoverApplyDetailWrapper
					.selectBySellerIdAndApplyNo(3383L, applyNo)
					.getApply_detail_no();
			
			String payBizNo = TestDataManager.randomStr(10);
			
			FastTurnoverService.verifyTurnoverApply(Arrays.asList(applyNo));
			
			FastTurnoverService.onApplyDetailExportPayEvent(applyDetailNo);
			
			FastTurnoverService.onApplyDetailImportPayEvent(applyDetailNo, payBizNo, 1);
			
			String accountid1 = AccountCallServiceV2.getAccountId(3383, 1);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					"10003.00"), BigDecimal.TEN);
			
			FastTurnoverService.autoRepayJob();
			
			Thread.sleep(3000);

			// 验证数据库结果
			TurnoverApply actualResult = turnoverApplyWrapper.selectByPrimaryKey(applyNo);
			
			// 会记录类似日志“买手20227567人民币账户金额1.00小于待还款金额10001.00，不能自动还款”
			Logger.verifyEquals(1, actualResult.getPay_status(),
					"验证Pay_status");
//			Logger.verifyEquals(2, actualResult.getRepay_status(),
//					"验证Repay_status");

			TurnoverApplyDetail actualTurnoverApplyDetail = turnoverApplyDetailWrapper.selectBySellerIdAndApplyNo(3383L,
					applyNo);
			
			Logger.verifyEquals(6, actualTurnoverApplyDetail.getStatus(),
					"验证status");
			
			turnoverApplyWrapper.updateToFinishByApplyNo(applyNo);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		AccountDefaultWapper wapper = new AccountDefaultWapper();
		
		wapper.selectAccountEntry(20227567, "FKT2020040600000041", 1).get(0)
				.getAmount();
	}
}

